package cn.frontop.funswitch.controller;

import cn.frontop.funswitch.common.Result;
import cn.frontop.funswitch.entity.TokenEntity;
import cn.frontop.funswitch.service.TokenService;
import cn.frontop.funswitch.util.CommonUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * 角色token表
 *
 * @author Yun
 * @email Yun@frontop.cn
 * @date 2022-10-19 15:41:15
 */
@RestController
@RequestMapping("/token")
@Api(tags = "第三方token管理")
public class TokenController {
    @Autowired
    private TokenService tokenService;


    /**
     * 查看第三方token管理列表
     *
     * @param pageNo   当前页码
     * @param pageSize 每页显示条数
     * @return 结果集
     */
    @GetMapping(value = "/list")
    @ApiOperation("查看第三方token管理列表")
    public Result<?> queryApiPathList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                      @RequestParam(name = "projectId", defaultValue = "1") Integer projectId) {
        return CommonUtil.entityPageList(tokenService, pageNo, pageSize, new QueryWrapper<TokenEntity>().lambda().eq(TokenEntity::getProjectId, projectId).orderByDesc(TokenEntity::getCreatedTime));
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("查询Token")
    public Result<TokenEntity> info(@PathVariable("id") Integer id) {
        TokenEntity token = tokenService.getById(id);
        return Result.ok(token);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("保存Token")
    public Result<String> save(@RequestBody TokenEntity token) {
        tokenService.save(token);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改Token")
    public Result<String> update(@RequestBody TokenEntity token) {
        token.setUpdatedTime(new Date());
        tokenService.removeCache(token.getId());
        tokenService.updateById(token);
        return Result.ok();
    }

    /**
     * 删除
     */
    @GetMapping("/delete/{id}")
    @ApiOperation("删除Token")
    public Result<String> delete(@PathVariable("id") Integer id) {
        tokenService.removeById(id);
        tokenService.removeCache(id);
        return Result.ok();
    }

}
